package flags

import (
	"flag"
	"io"
	"os"

	"law-api-master/conf"
	"law-api-master/log"
)

var (
	//config
	jsconfig = flag.String("config", "./config.json", "the config file for loading params")

	//lawTester params
	apiServerURL = flag.String("apiurl", "", "apiServer url")
	callbackURL  = flag.String("callback", "", "callback url")
	keyFile      = flag.String("keys", "keys.txt", "the path where keyfile storage")
)

func init() {
	flag.Parse()
}

//SetServerConfig config the params when we test lawServer
func SetTesterConfig() *conf.TesterConf {
	var serURL = *apiServerURL
	var callURL = *callbackURL
	var kfile = *keyFile

	if serURL == "" || callURL == "" {
		panic("can't find law server or callback server")
	}
	return conf.NewTesterConfig(serURL, callURL, kfile)
}

func ReadConfig() *conf.ConfigParams {
	file := *jsconfig
	return conf.ReadConfig(file)
}

func SetupLog() {
	output := io.Writer(os.Stderr)
	ostream := log.StreamHandler(output, log.TerminalFormat(true))
	glogger := log.NewGlogHandler(ostream)
	//log
	log.PrintOrigins(false)

	glogger.Verbosity(log.Lvl(3))
	glogger.Vmodule("")
	glogger.BacktraceAt("")
	log.Root().SetHandler(glogger)
}
